package com.ihealthway.all.interfaces.dwihealth.vo;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import javax.xml.bind.annotation.*;
import java.beans.Transient;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Bean class for DW customer design
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@Table("interf_dw_customer")
@XmlRootElement
@XmlType(propOrder = { "custNo", "name", "sex", "birthdayStr", "idType", "idNo", "companyId", "custSerItems" })
public class DWCustomer {

    private static final Log logger = Logs.get();
    private static final SimpleDateFormat DATE_FMT = new SimpleDateFormat("yyyy-MM-dd");

    @Name
    @Column("cust_no")
    private String custNo;
    @Column("cust_name")
    private String name;
    @Column("cust_sex")
    private String sex;
    @Column("cust_birthday")
    private Date birthday;
    private String birthdayStr;
    @Column("cust_id_type")
    private String idType;
    @Column("cust_id_no")
    private String idNo;
    @Column("cust_company_id")
    private String companyId;


    @Column("create_time")
    private Date createTime;
    @Column("update_time")
    private Date updateTime;
    @Column("data_status")
    private String dataStatus;

    private List<DWCustSerItem> custSerItems;

    @XmlElement(name = "CustNo")
    public String getCustNo() {
        return custNo;
    }

    public void setCustNo(String custNo) {
        this.custNo = custNo;
    }

    @XmlElement(name = "CusName")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(name = "CusSex")
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @XmlTransient
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @XmlElement(name = "CusIdType")
    public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    @XmlElement(name = "CusIdNo")
    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    @XmlElement(name = "CusCompanyId")
    public String getCompanyId() {
        return companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
    @XmlTransient
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    @XmlTransient
    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    @XmlTransient
    public String getDataStatus() {
        return dataStatus;
    }

    public void setDataStatus(String dataStatus) {
        this.dataStatus = dataStatus;
    }

    @Transient
    @XmlElement(name = "CusBirthday")
    public String getBirthdayStr() {
        return birthdayStr;
    }

    public void setBirthdayStr(String birthdayStr) {
        this.birthdayStr = birthdayStr;
    }

    @Transient
    @XmlElementWrapper(name = "ItemInfos")
    @XmlElement(name = "ItemInfo")
    public List<DWCustSerItem> getCustSerItems() {
        return custSerItems;
    }

    public void setCustSerItems(List<DWCustSerItem> custSerItems) {
        this.custSerItems = custSerItems;
    }

    public ErrorInfoElement valid() {
        ErrorInfoElement error = new ErrorInfoElement();

        error.setCustNo(this.custNo);
        if (Strings.isBlank(this.name)) {
            error.setErrorInfo("客户姓名不能为空");
            return error;
        }

        if (Strings.isBlank(this.sex)) {
            error.setErrorInfo("客户性别不能为空");
            return error;
        }

        String reg = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
        if (Strings.isBlank(this.birthdayStr) || !this.birthdayStr.matches(reg)) {
            error.setErrorInfo("客户生日不能为空或格式不对");
            return error;
        }

        if (Strings.isBlank(this.idType)) {
            error.setErrorInfo("客户证件类型不能为空");
            return error;
        }

        if (Strings.isBlank(this.idNo)) {
            error.setErrorInfo("客户证件号不能为空");
            return error;
        }

        if (Strings.isBlank(this.companyId)) {
            error.setErrorInfo("客户工作单位不能为空");
            return error;
        }

        try {
            this.birthday = DATE_FMT.parse(this.birthdayStr);
        } catch (ParseException e) {
            logger.error(e);
            error.setErrorInfo("客户生日格式不对");
            return error;
        }

        if (this.custSerItems==null || this.custSerItems.isEmpty()) {
            error.setErrorInfo("ItemInfos节点未找到信息");
            return error;
        }

        this.dataStatus = "1";
        this.createTime = new Date();

        return error;
    }

    @Override
    public String toString() {
        return "DWCustomer{" +
                "custNo='" + custNo + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", idType='" + idType + '\'' +
                ", idNo='" + idNo + '\'' +
                ", companyId='" + companyId + '\'' +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", dataStatus='" + dataStatus + '\'' +
                ", custSerItems='" + (custSerItems==null ? 0 : custSerItems.size()) + '\'' +
                '}';
    }
}
